package uicgas.register.GUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.ListSelectionModel;

import uicgas.globaldata.ControlMessage;
import uicgas.globaldata.Messages;
import uicgas.globaldata.Pump;
import uicgas.globaldata.SelfAnnouncement;
import uicgas.register.RegisterLogic;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Queue;

public class RegisterQueue extends JFrame
{

	private JPanel contentPane;
	final private JList assistanceList;
	private int index;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					//RegisterQueue frame = new RegisterQueue();
					//frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	//	public RegisterQueue()
	//	{
	//		setResizable(false);
	//		setTitle("UIC Gas - Customer Assistance Queue");
	//		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	//		setBounds(100, 100, 300, 500);
	//		contentPane = new JPanel();
	//		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	//		contentPane.setLayout(new BorderLayout(0, 0));
	//		setContentPane(contentPane);
	//		
	//		final JList assistanceList = new JList();
	//		assistanceList.addMouseListener(new MouseAdapter() {
	//			@Override
	//			public void mouseClicked(MouseEvent arg0) {
	//				if(arg0.getClickCount() == 1)
	//				{
	//					index = assistanceList.locationToIndex(arg0.getPoint());
	//					
	//				}
	//			}
	//		});
	//		assistanceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	//		contentPane.add(assistanceList, BorderLayout.CENTER);
	//		
	//		JLabel lblCustomerAssistanceQueue = new JLabel("Customer Assistance Queue");
	//		lblCustomerAssistanceQueue.setFont(new Font("Tahoma", Font.BOLD, 16));
	//		lblCustomerAssistanceQueue.setHorizontalAlignment(SwingConstants.CENTER);
	//		contentPane.add(lblCustomerAssistanceQueue, BorderLayout.NORTH);
	//		
	//		JPanel panel = new JPanel();
	//		contentPane.add(panel, BorderLayout.SOUTH);
	//		
	//		JButton btnOk = new JButton("OK");
	//		btnOk.addMouseListener(new MouseAdapter() {
	//			@Override
	//			public void mouseClicked(MouseEvent arg0) {
	//			}
	//		});
	//		panel.add(btnOk);
	//		
	//		JButton btnCancle = new JButton("Cancel");
	//		btnCancle.addMouseListener(new MouseAdapter() {
	//			@Override
	//			public void mouseClicked(MouseEvent arg0) {
	//			}
	//		});
	//		panel.add(btnCancle);
	//	}

	public RegisterQueue(final RegisterLogic register)
	{
		setResizable(false);
		setTitle("UIC Gas - Customer Assistance Queue");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 300, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		assistanceList = new JList();
		assistanceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//assistanceList.setListData(register.allPumps);
		assistanceList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getClickCount() == 1)
				{
					index = assistanceList.locationToIndex(arg0.getPoint());
					
				}
			}
		});
		contentPane.add(assistanceList, BorderLayout.CENTER);
		
		
		JLabel lblCustomerAssistanceQueue = new JLabel("Customer Assistance Queue");
		lblCustomerAssistanceQueue.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblCustomerAssistanceQueue.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblCustomerAssistanceQueue, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		
		JButton btnOk = new JButton("Remove");
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//return some pump value or send message to station
				assistanceList.setSelectedIndex(index);
				Pump thePump = (Pump) assistanceList.getSelectedValue();
				int id = thePump.getID()+1;
				if(JOptionPane.showConfirmDialog(null, "You have helped the customer at pump No. " + id + "?", "UIC Gas - Confirm Assistance", JOptionPane.YES_NO_OPTION)==0)
				{
					//System.out.println(assistanceList.getSelectedValue());
					SelfAnnouncement myAnnouncement = new SelfAnnouncement(register.getID(), thePump.getID());
					ControlMessage myMessage = new ControlMessage(Messages.DEQUEUEASSISTANCE, myAnnouncement);
					register.getCommunication().sendData(myMessage);
					register.getAssistanceQueue().getQueue().remove(thePump);
					JOptionPane.showMessageDialog(null,"Removed Pump No. " + id + ".","UIC Gas - Confirm Removal",JOptionPane.PLAIN_MESSAGE);
					exitThisScreen();
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Pump not removed from list.", "UIC Gas - Confirm Removal", JOptionPane.PLAIN_MESSAGE);
					exitThisScreen();
				}
			}
		});
		panel.add(btnOk);
		
		JButton btnCancle = new JButton("Close");
		btnCancle.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				exitThisScreen();
			}
		});
		panel.add(btnCancle);
	}

	public void updateList(Queue AssistanceQueue)
	{
		assistanceList.setListData(AssistanceQueue.toArray());
	}

	protected void exitThisScreen()
	{
		this.dispose();
	}

}
